{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "listed-vector",
   "metadata": {},
   "source": [
    "# Automatic Piecewise Linear Regression (APLR)\n",
    "\n",
    "Links to API References: [APLRRegressor](./python/api/APLRRegressor.ipynb), [APLRClassifier](./python/api/APLRClassifier.ipynb)\n",
    "\n",
    "*See the backing repository for APLR [here](https://github.com/ottenbreit-data-science/aplr).*\n",
    "\n",
    "<h2>Summary</h2>\n",
    "\n",
    "APLR produces inherently interpretable models. The relationship between the response and its explanatory variables are modeled with piecewise linear basis functions. The algorithm automatically handles variable selection, non-linear relationships and interactions. Empirical tests show that APLR is often able to compete with tree-based methods on predictiveness. Predictions from APLR models are usually smoother than those from tree-based methods due to the piecewise linear base learners. APLR can be used for regression tasks and classification tasks, including multiclass classification. The implementation is a light wrapper to the `aplr` package, adding the `explain_global` and `explain_local` methods so that APLR models can be interpreted in the same framework as for example EBMs.\n",
    "\n",
    "<h2>How it Works</h2>\n",
    "\n",
    "A brief introduction to APLR and how to tune APLR models can be found [here](https://github.com/ottenbreit-data-science/aplr/tree/main/documentation). The article \"Automatic piecewise linear regression\" [[1](aplr_article)] describes APLR in detail and can be found [here](https://rdcu.be/dz7bF). \n",
    "\n",
    "<h2>Code Example</h2>\n",
    "\n",
    "The following code will train a logistic regression for the breast cancer dataset. The visualizations provided will be for both global and local explanations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "chemical-warrior",
   "metadata": {},
   "outputs": [],
   "source": [
    "from interpret import set_visualize_provider\n",
    "from interpret.provider import InlineProvider\n",
    "\n",
    "set_visualize_provider(InlineProvider())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "liberal-aurora",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.datasets import load_breast_cancer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import roc_auc_score\n",
    "\n",
    "from interpret.glassbox import APLRClassifier\n",
    "from interpret import show\n",
    "\n",
    "seed = 42\n",
    "np.random.seed(seed)\n",
    "X, y = load_breast_cancer(return_X_y=True, as_frame=True)\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.20, random_state=seed\n",
    ")\n",
    "\n",
    "aplr = APLRClassifier(random_state=seed)\n",
    "aplr.fit(X_train, y_train, X_names=X_train.columns)\n",
    "\n",
    "auc = roc_auc_score(y_test, aplr.predict_class_probabilities(X_test)[:, 1])\n",
    "print(\"AUC: {:.3f}\".format(auc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3819596",
   "metadata": {},
   "outputs": [],
   "source": [
    "show(aplr.explain_global())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3819597",
   "metadata": {},
   "outputs": [],
   "source": [
    "show(aplr.explain_local(X_test.values[:5], y_test.values[:5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "declared-telephone",
   "metadata": {},
   "source": [
    "<h2>Further Resources</h2>\n",
    "\n",
    "Below are more detailed code examples from the backing repository for APLR. This will also work when using APLR through `InterpretML` if you change `from aplr import` to `from interpret.glassbox import`.\n",
    "- [Regression code example](https://github.com/ottenbreit-data-science/aplr/blob/main/examples/train_aplr_regression.py)\n",
    "- [Classification code example](https://github.com/ottenbreit-data-science/aplr/blob/main/examples/train_aplr_classification.py)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "municipal-tooth",
   "metadata": {},
   "source": [
    "<h2>Bibliography</h2>\n",
    "\n",
    "(aplr_article)=\n",
    "[1] von Ottenbreit, M., De Bin, R. Automatic piecewise linear regression. Comput Stat 39, 1867–1907 (2024)."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
